<template>
  <div class="lNumber">
    <div class="label">
      <span v-if="props?.rule?.require">*</span>
      {{props.label}}
    </div>
    <div class="action">
      <div class="btn" @click="reduce">-</div>
      <div class="num">{{props.modelValue}}</div>
      <div class="btn" @click="plus">+</div>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  modelValue: {
    type: Number,
    default: 1
  },
  label: '',
  rule: { require: false, reg: '', message: '' },
  max: { type: Number, default: undefined },
  min: { type: Number, default: undefined }
})

const emits = defineEmits('update:modelValue')
// 减
function reduce() {
  console.log(props.label, "减")
  if (props.min) {
    emits('update:modelValue', props.modelValue - 1 < props.min ? props.min : props.modelValue - 1)
  } else {
    emits('update:modelValue', props.modelValue - 1)
  }
}
// 加
function plus() {
  if (props.max) {
    emits('update:modelValue', props.modelValue + 1 > props.max ? props.max : props.modelValue + 1)
  } else {
    emits('update:modelValue', props.modelValue + 1)
  }
}

setTimeout(() => {
  if (props.modelValue > props.max) {
    emits('update:modelValue', props.max)
  }
  if (props.modelValue < props.max) {
    emits('update:modelValue', props.min)
  }
}, 10)

</script>

<style lang="scss" scoped>
.lNumber {
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .label {
    font-size: 28rpx;
    font-weight: 400;
    color: #333333;
  }
  .action {
    display: flex;
    .btn {
      width: 48rpx;
      height: 48rpx;
      background: #ecf3ff;
      border-radius: 16rpx;
      color: #468eff;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .num {
      min-width: 80rpx;
      text-align: center;

      font-size: 28rpx;
      font-family: PingFang SC, PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #333333;
      position: relative;
      top: 4rpx;
    }
  }
}
</style>